﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IRepository.LY;
using DFGK.IService.LY;
using DFGK_WMS.Model.Models.LyModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK.Service.LY
{
    public class MatchingEquipmentService : BaseService, IMatchingEquipmentService
    {
        private readonly IMatchingEquipmentRepository _matchingEquipmentRepository;
        private readonly IMatchingEquipmentDetailRepository _matchingEquipmentDetailRepository;
        private readonly IUnitOfWork unitOfWork;
        public MatchingEquipmentService(IMapper mapper, IMatchingEquipmentRepository matchingEquipmentRepository, IUnitOfWork unitOfWork, IMatchingEquipmentDetailRepository matchingEquipmentDetailRepository = null) : base(mapper)
        {
            _matchingEquipmentRepository = matchingEquipmentRepository;
            this.unitOfWork = unitOfWork;
            _matchingEquipmentDetailRepository = matchingEquipmentDetailRepository;
        }

        public bool AddOrModefiyMatchingEquipment(MatchingEquipment model, out string errorMsg)
        {
            bool result = false;
            errorMsg = string.Empty;
            unitOfWork.BeginTransaction();
            try
            {
                if (model.Id > 0)
                {
                    _matchingEquipmentRepository.UpdEntity(model);
                    result = unitOfWork.SaveChangesEntity();
                    if (!result)
                    {
                        errorMsg = "添加失败";
                        unitOfWork.RollbackChanges();
                        return false;
                    }
                    List<long> ids = model.MatchingEquipmentDetails.Where(t => t.Id > 0).Select(x => x.Id).ToList();
                    if (ids != null && ids.Count > 0)
                    {
                        var matchingDetailList = _matchingEquipmentDetailRepository.GetEntityWhereAll(t => !ids.Contains(t.Id)).ToList();
                        if (matchingDetailList != null && matchingDetailList.Count > 0)
                        {
                            _matchingEquipmentDetailRepository.DelEntityList(matchingDetailList);
                            result = unitOfWork.SaveChangesEntity();
                            if (!result)
                            {
                                errorMsg = "添加失败";
                                unitOfWork.RollbackChanges();
                                return false;
                            }
                        }

                    }
                    else
                    {
                        var matchingDetailList = _matchingEquipmentDetailRepository.GetEntityWhereAll(t => t.MatchId == model.Id).ToList();
                        if (matchingDetailList != null && matchingDetailList.Count > 0)
                        {
                            _matchingEquipmentDetailRepository.DelEntityList(matchingDetailList);
                            result = unitOfWork.SaveChangesEntity();
                            if (!result)
                            {
                                errorMsg = "添加失败";
                                unitOfWork.RollbackChanges();
                                return false;
                            }
                        }

                    }
                }
                else
                {
                    _matchingEquipmentRepository.AddEntity(model);
                    result = unitOfWork.SaveChangesEntity();
                    if (!result)
                    {
                        errorMsg = "添加失败";
                        unitOfWork.RollbackChanges();
                        return false;
                    }
                }
                foreach (var detail in model.MatchingEquipmentDetails)
                {
                    if (detail.Id > 0)
                    {
                        _matchingEquipmentDetailRepository.UpdEntity(detail);
                        result = unitOfWork.SaveChangesEntity();
                        if (!result)
                        {
                            errorMsg = "添加失败";
                            unitOfWork.RollbackChanges();
                            return false;
                        }
                    }
                    else
                    {
                        _matchingEquipmentDetailRepository.AddEntity(detail);
                        result = unitOfWork.SaveChangesEntity();
                        if (!result)
                        {
                            errorMsg = "添加失败";
                            unitOfWork.RollbackChanges();
                            return false;
                        }
                    }
                }
                unitOfWork.CommitChanges();
                return true;
            }
            catch (Exception ex)
            {
                errorMsg = ex.Message;
                unitOfWork.RollbackChanges();
                return false;
            }
        }

        public bool DelMatchingEquipment(long matchingEquipmentId)
        {
            var Quote = _matchingEquipmentRepository.QueryEntityById(matchingEquipmentId);
            if (Quote == null)
            {
                return false;
            }
            _matchingEquipmentRepository.DelEntity(Quote);
            return unitOfWork.SaveChangesEntity();
        }

        public List<MatchingEquipment> GetMatchingEquipmentData(string? name, out int totalCount, int pageIndex = 1, int pageSize = 15)
        {
            var list = _matchingEquipmentRepository.GetEntityAlls();
            if (!string.IsNullOrEmpty(name))
            {
                list = list.Where(d => d.Name.Contains(name));
            }
            totalCount = list.Count();
            list = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).OrderByDescending(d => d.Id);
            return list.ToList();
        }

        public MatchingEquipment? GetMatchingEquipment(long matchingEquipmentId)
        {
            var match = _matchingEquipmentRepository.QueryEntityById(matchingEquipmentId);
            if (match != null)
                match.MatchingEquipmentDetails = _matchingEquipmentDetailRepository.GetEntityWhereAll(t => t.MatchId == match.Id).ToList();
            return match;
        }

    }
}
